package edu.hniu.skilltest.demo06;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Locale;

import edu.hniu.skilltest.R;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnLogin,btnRegister;
    private EditText etUser,etPwd;
    private MyDBHelper helper;
    private SQLiteDatabase database;

    private CheckBox cbSave;//保存账号密码
    private SharedPreferences sp; //用于保存账号和密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        sp = getSharedPreferences("UserInfo", 0);
        initView();
    }

    private void initView() {
        btnLogin = findViewById(R.id.login_btn_login_06);
        btnLogin.setOnClickListener(this);
        etUser = findViewById(R.id.login_edit_account_06);
        etPwd = findViewById(R.id.login_edit_pwd_06);
        cbSave = findViewById(R.id.login_cb_savepwd_07);

        String user = sp.getString("account","");
        String pwd = sp.getString("password", "");
        etUser.setText(user);
        etPwd.setText(pwd);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_btn_login_06:
                String user = etUser.getText().toString().trim();
                String pwd = etPwd.getText().toString().trim();
                if (TextUtils.isEmpty(user) || TextUtils.isEmpty(pwd)) {
                    Toast.makeText(this, "用户名或密码不能为空!", Toast.LENGTH_SHORT).show();
                    break;
                }
                if (verification(user, pwd)){
                    Toast.makeText(this, "账号密码正确!", Toast.LENGTH_SHORT).show();
                    SharedPreferences.Editor editor = sp.edit();
                    if (cbSave.isChecked()){//如果保存密码复选框被选中
                        editor.putString("account", user);
                        editor.putString("password", pwd);
                        //提交保存
                        editor.commit();
                        Log.d("TAG", "账号密码保存成功");
                    }else {
                        editor.clear();
                        editor.commit();
                        Log.d("TAG", "已清空");
                    }
                }else{
                    Toast.makeText(this, "账号密码不正确!", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    public  boolean verification(String user, String pwd){
        helper = new MyDBHelper(this, 1);
        database = helper.getReadableDatabase();
        String sql = "select * from T_User_infos where User_nickname =? and User_pwd = ?";
        Cursor cursor = database.rawQuery(sql, new String[]{user, pwd});
        if (cursor.moveToFirst()){
            cursor.close();
            return true;
        }
        return false;
    }
}